package com.xlh.enums;

import com.xlh.properties.ResourcesProperties;
import com.xlh.util.SpringContextUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.helpers.MessageFormatter;

import java.io.File;

/**
 * 本地资源配置枚举
 * <p>
 * Created by lx on 2018/6/7.
 */
@Getter
@AllArgsConstructor
public enum LocalResourcesEnum {

    SYSTEM_FILE(1, "系统文件") {
        @Override
        public String getLocalUploadPath() {
            return config.getSystemFile().getLocalDir();
        }

        @Override
        public String getMappingUrl() {
            return config.getSystemFile().getMappingUrl();
        }

        @Override
        public String getProxyPath() {
            return config.getSystemFile().getProxyDir();
        }
    },
    CHAPTER(2, "章节") {
        @Override
        public String getLocalUploadPath() {
            return StringUtils.join(config.getCourse().getLocalDir(), "/{}/chapter-resources");
        }

        @Override
        public String getMappingUrl() {
            return StringUtils.join(config.getCourse().getMappingUrl(), "/{}/chapter-resources");
        }

        @Override
        public String getProxyPath() {
            return StringUtils.join(config.getCourse().getProxyDir(), "/{}/chapter-resources");
        }

    },
    CASES(3, "案例库") {
        @Override
        public String getLocalUploadPath() {
            return config.getCases().getLocalDir();
        }

        @Override
        public String getMappingUrl() {
            return config.getCases().getMappingUrl();
        }

        @Override
        public String getProxyPath() {
            return config.getCases().getProxyDir();
        }

    },
    MARKDOWN_PICTURE(4, "markdown图片") {
        @Override
        public String getLocalUploadPath() {
            return config.getMarkdownPicture().getLocalDir();
        }

        @Override
        public String getMappingUrl() {
            return config.getMarkdownPicture().getMappingUrl();
        }

        @Override
        public String getProxyPath() {
            return config.getMarkdownPicture().getProxyDir();
        }

    },
    PAPER(5, "考试试卷") {
        @Override
        public String getLocalUploadPath() {
            return config.getPaper().getLocalDir();
        }

        @Override
        public String getMappingUrl() {
            return config.getPaper().getMappingUrl();
        }

        @Override
        public String getProxyPath() {
            return config.getPaper().getProxyDir();
        }

    },
    COURSE_TEMPLATE(6, "实验课报告模板") {
        @Override
        public String getLocalUploadPath() {
            return StringUtils.join(config.getTemplate().getLocalDir(), "/course");
        }

        @Override
        public String getMappingUrl() {
            return StringUtils.join(config.getTemplate().getMappingUrl(), "/course");
        }

        @Override
        public String getProxyPath() {
            return StringUtils.join(config.getTemplate().getProxyDir(), "/course");
        }

    },
    TEST_TEMPLATE(7, "实验考试报告模板") {
        @Override
        public String getLocalUploadPath() {
            return StringUtils.join(config.getTemplate().getLocalDir(), "/test");
        }

        @Override
        public String getMappingUrl() {
            return StringUtils.join(config.getTemplate().getMappingUrl(), "/test");
        }

        @Override
        public String getProxyPath() {
            return StringUtils.join(config.getTemplate().getProxyDir(), "/test");
        }

    },
    TRAINING(8, "环境实训") {
        @Override
        public String getLocalUploadPath() {
            return config.getTraining().getLocalDir();
        }

        @Override
        public String getMappingUrl() {
            return config.getTemplate().getMappingUrl();
        }

        @Override
        public String getProxyPath() {
            return config.getTemplate().getProxyDir();
        }

    },
    REPORT(9, "实验报告") {
        @Override
        public String getLocalUploadPath() {
            return config.getReport().getLocalDir();
        }

        @Override
        public String getMappingUrl() {
            return config.getReport().getMappingUrl();
        }

        @Override
        public String getProxyPath() {
            return config.getReport().getProxyDir();
        }

    },
    NOTES(10, "笔记") {
        @Override
        public String getLocalUploadPath() {
            return config.getNotes().getLocalDir();
        }

        @Override
        public String getMappingUrl() {
            return config.getNotes().getMappingUrl();
        }

        @Override
        public String getProxyPath() {
            return config.getNotes().getProxyDir();
        }

    },
    TEST_REPOSITORY(11, "考试题库") {
        @Override
        public String getLocalUploadPath() {
            return StringUtils.join(config.getCourse().getLocalDir(), "/{}/repository");
        }

        @Override
        public String getMappingUrl() {
            return StringUtils.join(config.getCourse().getMappingUrl(), "/{}/repository");
        }

        @Override
        public String getProxyPath() {
            return StringUtils.join(config.getCourse().getProxyDir(), "/{}/repository");
        }

    },
    COURSE(12, "课程") {
        @Override
        public String getLocalUploadPath() {
            return StringUtils.join(config.getCourse().getLocalDir(), "/{}");
        }

        @Override
        public String getMappingUrl() {
            return StringUtils.join(config.getCourse().getMappingUrl(), "/{}");
        }

        @Override
        public String getProxyPath() {
            return StringUtils.join(config.getCourse().getProxyDir(), "/{}");
        }

    },
    PDF(13, "PDF归档") {
        @Override
        public String getLocalUploadPath() {
            return StringUtils.join(config.getPdf().getLocalDir());
        }

        @Override
        public String getMappingUrl() {
            return StringUtils.join(config.getPdf().getMappingUrl());
        }

        @Override
        public String getProxyPath() {
            return StringUtils.join(config.getPdf().getProxyDir());
        }
    },
    COURSE_TMP(14, "课程临时文件") {
        @Override
        public String getLocalUploadPath() {
            return StringUtils.join(config.getCourseTmp().getLocalDir(), "/{}");
        }

        @Override
        public String getMappingUrl() {
            return null;
        }

        @Override
        public String getProxyPath() {
            return null;
        }
    },
    PRACTICE(14, "随堂练习") {
        @Override
        public String getLocalUploadPath() {
            return config.getPractice().getLocalDir();
        }

        @Override
        public String getMappingUrl() {
            return config.getPractice().getMappingUrl();
        }

        @Override
        public String getProxyPath() {
            return config.getPractice().getProxyDir();
        }
    };

    private Integer code;

    private String message;

    private static ResourcesProperties config = SpringContextUtil.getBean(ResourcesProperties.class);

    public abstract String getLocalUploadPath();

    public abstract String getMappingUrl();

    public abstract String getProxyPath();

    public String getLocalUploadPath(String fileName) {
        return StringUtils.join(getLocalUploadPath(), File.separator, fileName);
    }

    public String getLocalUploadPath(String[] param, String fileName) {
        String path = getLocalUploadPath();
        if (param != null) {
            path = MessageFormatter.arrayFormat(path, param).getMessage();
        }
        return StringUtils.join(path, File.separator, fileName);
    }

    public String getMappingUrl(String fileName) {
        if (StringUtils.isBlank(fileName)) return fileName;
        return StringUtils.join(getMappingUrl(), "/", fileName);
    }

    public String getMappingUrl(String[] param, String fileName) {
        if (StringUtils.isBlank(fileName)) return fileName;
        String path = getMappingUrl();
        if (param != null) {
            path = MessageFormatter.arrayFormat(path, param).getMessage();
        }
        return StringUtils.join(path, "/", fileName);
    }

}
